﻿/*  ----------------------------------------------------------------------------
 *  Author:
 *  Chandra Shekhar Tripathi
 *  ----------------------------------------------------------------------------
 *  Product Name:
 *  DbDocumenter
 *  ----------------------------------------------------------------------------
 *  Year:
 *  2009
 *  ----------------------------------------------------------------------------
 *  File:
 *  XMLManager.cs
 *  ----------------------------------------------------------------------------
 */
namespace DBDocumenter.BLL.Services
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;

    using DBDocumenter.BLL.Entities;
    using DBDocumenter.Exception;
    using DBDocumenter.Helper;
    using DBDocumenter.Resources;

    class XMLManager
    {
        #region Fields

        private static XMLManager instance = new XMLManager();

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="XMLManager"/> class.
        /// </summary>
        public XMLManager()
        {
        }

        #endregion Constructors

        #region Properties

        /// <summary>
        /// Gets or sets the instance.
        /// </summary>
        /// <value>The instance.</value>
        public static XMLManager Instance
        {
            get { return XMLManager.instance; }
            set { XMLManager.instance = value; }
        }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Gets the SQL in XML format.
        /// </summary>
        /// <param name="sql">The SQL.</param>
        /// <returns></returns>
        public static string GetSQLInXMLFormat(string sql)
        {
            sql = sql.Replace("&", "&amp;");
            sql = sql.Replace("<", "&lt;");
            sql = sql.Replace(">", "&gt;");
            sql = sql.Replace("''", "&quot;");
            return sql;
        }

        /// <summary>
        /// Gets the tree menu XML.
        /// </summary>
        /// <returns></returns>
        public static string GetTreeMenuXML()
        {
            string xml = string.Empty;
            xml = xml + "<Menus>";
            xml = xml + "<DatabaseName>" + Utility.databaseName + "</DatabaseName>";

            xml = xml + "<Menu>";
            xml = xml + "<Name>Server Information</Name>";
            xml = xml + "<Type>file</Type>";
            xml = xml + "<FileName>DatabaseList.htm</FileName>";
            xml = xml + "</Menu>";

            //For Tables
            xml = xml + "<Menu>";
            xml = xml + "<Name>Tables</Name>";
            if (Utility.tables.Count > 0)
            {
                xml = xml + "<Type>folder</Type>";
                xml = xml + "<FileName>TableList.htm</FileName>";
            }
            else
            {
                xml = xml + "<Type>File</Type>";
                xml = xml + "<FileName>./Default/error.htm</FileName>";
            }
            foreach (Table table in Utility.tables)
            {
                xml = xml + "<Item>";
                xml = xml + "<Name>" + table.Name + "</Name>";
                xml = xml + "<FileName>" + table.Name + ".htm" + "</FileName>";
                xml = xml + "</Item>";
            }
            xml = xml + "</Menu>";

            //For Procedures
            xml = xml + "<Menu>";
            xml = xml + "<Name>Procedures</Name>";
            if (Utility.storedProcedures.Count > 0)
            {
                xml = xml + "<Type>folder</Type>";
                xml = xml + "<FileName>Procedures.htm</FileName>";
            }
            else
            {
                xml = xml + "<Type>File</Type>";
                xml = xml + "<FileName>./Default/error.htm</FileName>";
            }
            foreach (StoredProcedure procedure in Utility.storedProcedures)
            {
                xml = xml + "<Item>";
                xml = xml + "<Name>" + procedure.Name + "</Name>";
                xml = xml + "<FileName>" + procedure.Name + ".htm" + "</FileName>";
                xml = xml + "</Item>";
            }
            xml = xml + "</Menu>";

            //For Views
            xml = xml + "<Menu>";
            xml = xml + "<Name>Views</Name>";
            if (Utility.views.Count > 0)
            {
                xml = xml + "<Type>folder</Type>";
                xml = xml + "<FileName>Views.htm</FileName>";
            }
            else
            {
                xml = xml + "<Type>File</Type>";
                xml = xml + "<FileName>./Default/error.htm</FileName>";
            }
            foreach (View view in Utility.views)
            {
                xml = xml + "<Item>";
                xml = xml + "<Name>" + view.Name + "</Name>";
                xml = xml + "<FileName>" + view.Name + ".htm" + "</FileName>";
                xml = xml + "</Item>";
            }
            xml = xml + "</Menu>";

            //For Functions
            xml = xml + "<Menu>";
            xml = xml + "<Name>Functions</Name>";
            if (Utility.functions.Count > 0)
            {
                xml = xml + "<Type>folder</Type>";
                xml = xml + "<FileName>Functions.htm</FileName>";
            }
            else
            {
                xml = xml + "<Type>File</Type>";
                xml = xml + "<FileName>./Default/error.htm</FileName>";
            }
            foreach (Function function in Utility.functions)
            {
                xml = xml + "<Item>";
                xml = xml + "<Name>" + function.Name + "</Name>";
                xml = xml + "<FileName>" + function.Name + ".htm" + "</FileName>";
                xml = xml + "</Item>";
            }
            xml = xml + "</Menu>";

            xml = xml + "</Menus>";
            return xml;
        }

        /// <summary>
        /// Gets the XML.
        /// </summary>
        /// <param name="servers">The servers.</param>
        /// <returns></returns>
        public static string GetXML(List<ServerInfo> servers)
        {
            try
            {
                string xml = string.Empty;
                xml = xml + "<Databases>";
                foreach (ServerInfo server in servers)
                {
                    xml = xml + "<Database>";
                    xml = xml + "<DbID>" + server.DbID + "</DbID>";
                    xml = xml + "<DbName>" + server.DbName + "</DbName>";
                    xml = xml + "<DbCreateDate>" + server.DbCreateDate + "</DbCreateDate>";
                    xml = xml + "</Database>";
                }
                xml = xml + "</Databases>";
                return xml;
            }
            catch (System.Exception ex)
            {
                Logger.WriteLog(LogType.BusinessLayer, LogLevel.Warn, ex.ToString());
                return string.Empty;
            }
        }

        /// <summary>
        /// Gets the XML.
        /// </summary>
        /// <param name="tables">The tables.</param>
        /// <returns></returns>
        public static string GetXML(List<Table> tables)
        {
            try
            {
                string xml = string.Empty;
                xml = xml + "<Tables>";
                foreach (Table table in tables)
                {
                    xml = xml + "<Table>";
                    xml = xml + "<Name>" + table.Name + "</Name>";
                    xml = xml + "<Schema>" + table.Schema + "</Schema>";
                    xml = xml + "<Type>" + table.Type + "</Type>";
                    xml = xml + "</Table>";
                }
                xml = xml + "</Tables>";
                return xml;
            }
            catch (System.Exception ex)
            {
                Logger.WriteLog(LogType.BusinessLayer, LogLevel.Warn, ex.ToString());
                return string.Empty;
            }
        }

        /// <summary>
        /// Gets the XML.
        /// </summary>
        /// <param name="table">The table.</param>
        /// <returns></returns>
        public static string GetXML(Table table)
        {
            try
            {
                string xml = string.Empty;
                xml = xml + "<Table>";
                xml = xml + "<TableProperties><Name>Name</Name><Value>" + table.Name + "</Value></TableProperties>";
                xml = xml + "<TableProperties><Name>Schema</Name><Value>" + table.Schema + "</Value></TableProperties>";
                xml = xml + "<TableProperties><Name>Type</Name><Value>" + table.Type + "</Value></TableProperties>";
                foreach (Column column in table.Colunms)
                {
                    xml = xml + "<Column>";
                    xml = xml + "<Name>" + column.Name + "</Name>";
                    xml = xml + "<OrdinalPosition>" + column.OrdinalPosition + "</OrdinalPosition>";
                    xml = xml + "<DataSize>" + column.DataSize + "</DataSize>";
                    xml = xml + "<DataType>" + column.DataType + "</DataType>";
                    xml = xml + "<IsNullable>" + column.IsNullable.ToString() + "</IsNullable>";
                    xml = xml + "<Key>" + column.Key + "</Key>";
                    xml = xml + "</Column>";
                }
                xml = xml + "</Table>";
                return xml;
            }
            catch (System.Exception ex)
            {
                Logger.WriteLog(LogType.BusinessLayer, LogLevel.Warn, ex.ToString());
                return string.Empty;
            }
        }

        /// <summary>
        /// Gets the XML.
        /// </summary>
        /// <param name="storedProcedures">The stored procedures.</param>
        /// <returns></returns>
        public static string GetXML(List<StoredProcedure> storedProcedures)
        {
            try
            {
                string xml = string.Empty;
                xml = xml + "<Procedures>";
                foreach (StoredProcedure procedure in storedProcedures)
                {
                    xml = xml + "<Procedure>";
                    xml = xml + "<Name>" + procedure.Name + "</Name>";
                    xml = xml + "<Schema>" + procedure.Schema + "</Schema>";
                    xml = xml + "<CreatedDate>" + procedure.CreatedDate + "</CreatedDate>";
                    xml = xml + "<LastAlteredDate>" + procedure.LastAlteredDate + "</LastAlteredDate>";
                    xml = xml + "</Procedure>";
                }
                xml = xml + "</Procedures>";
                return xml;
            }
            catch (System.Exception ex)
            {
                Logger.WriteLog(LogType.BusinessLayer, LogLevel.Warn, ex.ToString());
                return null;
            }
        }

        /// <summary>
        /// Gets the XML.
        /// </summary>
        /// <param name="procedure">The procedure.</param>
        /// <returns></returns>
        public static string GetXML(StoredProcedure procedure)
        {
            try
            {
                string xml = string.Empty;
                xml = xml + "<Procedure>";
                xml = xml + "<ProcedureProperties><Name>Name</Name><Value>" + procedure.Name + "</Value></ProcedureProperties>";
                xml = xml + "<ProcedureProperties><Name>Schema</Name><Value>" + procedure.Schema + "</Value></ProcedureProperties>";
                xml = xml + "<ProcedureProperties><Name>Creation Date</Name><Value>" + procedure.CreatedDate + "</Value></ProcedureProperties>";
                xml = xml + "<ProcedureProperties><Name>Altered Date</Name><Value>" + procedure.LastAlteredDate + "</Value></ProcedureProperties>";
                foreach (Parameter spParameter in procedure.Parameters)
                {
                    xml = xml + "<ProcedureParameter>";
                    xml = xml + "<OrdinalPosition>" + spParameter.OrdinalPosition + "</OrdinalPosition>";
                    xml = xml + "<Name>" + spParameter.Name + "</Name>";
                    xml = xml + "<DataType>" + spParameter.DataType + "</DataType>";
                    xml = xml + "<Mode>" + spParameter.Mode + "</Mode>";
                    xml = xml + "</ProcedureParameter>";
                }
                xml = xml + "<ProcedureSQL>" + GetSQLInXMLFormat(procedure.Description) + "</ProcedureSQL>";
                xml = xml + "</Procedure>";
                return xml;
            }
            catch (System.Exception ex)
            {
                Logger.WriteLog(LogType.BusinessLayer, LogLevel.Warn, ex.ToString());
                return string.Empty;
            }
        }

        /// <summary>
        /// Gets the XML.
        /// </summary>
        /// <param name="functions">The functions.</param>
        /// <returns></returns>
        public static string GetXML(List<Function> functions)
        {
            try
            {
                string xml = string.Empty;
                xml = xml + "<Functions>";
                foreach (Function function in functions)
                {
                    xml = xml + "<Function>";
                    xml = xml + "<Name>" + function.Name + "</Name>";
                    xml = xml + "<Schema>" + function.Schema + "</Schema>";
                    xml = xml + "<CreatedDate>" + function.CreatedDate + "</CreatedDate>";
                    xml = xml + "<LastAlteredDate>" + function.LastAlteredDate + "</LastAlteredDate>";
                    xml = xml + "</Function>";
                }
                xml = xml + "</Functions>";
                return xml;
            }
            catch (System.Exception ex)
            {
                Logger.WriteLog(LogType.BusinessLayer, LogLevel.Warn, ex.ToString());
                return string.Empty;
            }
        }

        /// <summary>
        /// Gets the XML.
        /// </summary>
        /// <param name="function">The function.</param>
        /// <returns></returns>
        public static string GetXML(Function function)
        {
            try
            {
                string xml = string.Empty;
                xml = xml + "<Function>";
                xml = xml + "<FunctionProperties><Name>Name</Name><Value>" + function.Name + "</Value></FunctionProperties>";
                xml = xml + "<FunctionProperties><Name>Schema</Name><Value>" + function.Schema + "</Value></FunctionProperties>";
                xml = xml + "<FunctionProperties><Name>Creation Date</Name><Value>" + function.CreatedDate + "</Value></FunctionProperties>";
                xml = xml + "<FunctionProperties><Name>Altered Date</Name><Value>" + function.LastAlteredDate + "</Value></FunctionProperties>";
                foreach (Parameter functionParameter in function.Parameters)
                {
                    xml = xml + "<FunctionParameter>";
                    xml = xml + "<OrdinalPosition>" + functionParameter.OrdinalPosition + "</OrdinalPosition>";
                    xml = xml + "<Name>" + functionParameter.Name + "</Name>";
                    xml = xml + "<DataType>" + functionParameter.DataType + "</DataType>";
                    xml = xml + "<Mode>" + functionParameter.Mode + "</Mode>";
                    xml = xml + "</FunctionParameter>";
                }
                xml = xml + "<FunctionSQL>" + GetSQLInXMLFormat(function.Description) + "</FunctionSQL>";
                xml = xml + "</Function>";
                return xml;
            }
            catch (System.Exception ex)
            {
                Logger.WriteLog(LogType.BusinessLayer, LogLevel.Warn, ex.ToString());
                return string.Empty;
            }
        }

        /// <summary>
        /// Gets the XML.
        /// </summary>
        /// <param name="views">The views.</param>
        /// <returns></returns>
        public static string GetXML(List<View> views)
        {
            try
            {
                string xml = string.Empty;
                xml = xml + "<Views>";
                foreach (View view in views)
                {
                    xml = xml + "<View>";
                    xml = xml + "<Name>" + view.Name + "</Name>";
                    xml = xml + "<Schema>" + view.Schema + "</Schema>";
                    xml = xml + "<CheckSchema>" + view.CheckSchema + "</CheckSchema>";
                    xml = xml + "<IsUpdateble>" + view.IsUpdateble + "</IsUpdateble>";
                    xml = xml + "</View>";
                }
                xml = xml + "</Views>";
                return xml;
            }
            catch (System.Exception ex)
            {
                Logger.WriteLog(LogType.BusinessLayer, LogLevel.Warn, ex.ToString());
                return string.Empty;
            }
        }

        /// <summary>
        /// Gets the XML.
        /// </summary>
        /// <param name="view">The view.</param>
        /// <returns></returns>
        public static string GetXML(View view)
        {
            try
            {
                string xml = string.Empty;
                xml = xml + "<View>";
                xml = xml + "<ViewProperties><Name>Name</Name><Value>" + view.Name + "</Value></ViewProperties>";
                xml = xml + "<ViewProperties><Name>Schema</Name><Value>" + view.Schema + "</Value></ViewProperties>";
                xml = xml + "<ViewProperties><Name>Check Option</Name><Value>" + view.CheckSchema + "</Value></ViewProperties>";
                xml = xml + "<ViewProperties><Name>Is Updatable</Name><Value>" + view.IsUpdateble + "</Value></ViewProperties>";
                foreach (Column column in view.Colunms)
                {
                    xml = xml + "<Column>";
                    xml = xml + "<Name>" + column.Name + "</Name>";
                    xml = xml + "<OrdinalPosition>" + column.OrdinalPosition + "</OrdinalPosition>";
                    xml = xml + "<DataSize>" + column.DataSize + "</DataSize>";
                    xml = xml + "<DataType>" + column.DataType + "</DataType>";
                    xml = xml + "<IsNullable>" + column.IsNullable.ToString() + "</IsNullable>";
                    xml = xml + "<Key>" + column.Key + "</Key>";
                    xml = xml + "</Column>";
                }
                xml = xml + "<ViewSQL>" + GetSQLInXMLFormat(view.Description) + "</ViewSQL>";
                xml = xml + "</View>";
                return xml;
            }
            catch (System.Exception ex)
            {
                Logger.WriteLog(LogType.BusinessLayer, LogLevel.Warn, ex.ToString());
                return string.Empty;
            }
        }

        /// <summary>
        /// Writes the XML.
        /// </summary>
        /// <param name="xmlFileName">Name of the XML file.</param>
        /// <param name="xmlFile">The XML file.</param>
        public static void WriteXML(string xmlFileName, string xmlFile)
        {
            try { File.WriteAllText(@"xml\" + xmlFileName + PLResources.XMLExtention, xmlFile, Encoding.UTF8); }
            catch (System.Exception ex)
            {
                Logger.WriteLog(LogType.BusinessLayer, LogLevel.Error, ex.ToString());
            }
        }

        #endregion Methods
    }
}